import { Prefab, resources } from "cc";
import { Hint } from "./float.hint";

export enum EFloat {
	HINT,
	FRUIT_DIAMOND,
	FRUIT_SPECIAL,
}

const CONFIG = {
	[EFloat.HINT]: {
		component: Hint,
		path: 'hint',
		script: './float.official.hint',
		prefab: null,
	},
	[EFloat.FRUIT_DIAMOND]: {
		component: Hint,
		path: 'fruit-diamond',
		script: './float.fruit.diamond',
		prefab: null,
	},
	[EFloat.FRUIT_SPECIAL]: {
		component: Hint,
		path: 'fruit-special',
		script: './float.fruit.special',
		prefab: null,
	},
}

Reflect.ownKeys(CONFIG).forEach(key=>{
	let conf = CONFIG[key];
	conf.path = 'prefab/float/' + conf.path;
});

export async function GetFloatPrefab(p: EFloat): Promise<Prefab> {

	let conf = CONFIG[p];
	if(!conf.prefab){
		conf.prefab = await mtec.cc.loadResAsync(conf.path, Prefab, resources);
	}

	return conf.prefab;
}

export async function GetFloatComponent<F extends EFloat>(p: F): Promise<(typeof CONFIG)[F]['component']> {
	let conf = CONFIG[p];
	if(!conf.component){
		conf.component = await import(conf.script).then(m=>m.default);
	}

	return conf.component;
}

export type FloatCtor<F extends EFloat> = typeof CONFIG[F]['component'];

export type FloatIns<F extends EFloat> = InstanceType<FloatCtor<F>>;
